/**
 * File name:      MessageSourceAdapter.java
 * Date:           2013-4-2
 * Description:    // 用于详细说明此程序文件完成的主要功
 *                 // 能与其他模块或函数的接口，输出值、
 *                 // 取值范围、含义及参数间的关系
 * Modify History:     Date             Programmer       Notes
 *                    ---------        ---------------  ---------
 *                    2013-4-2            胡少磊                           Initial
 **********************************************************************/
package com.wy.framework.adapter;

import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

/**
 * Created on 2013-4-2
 * <p>Title:       [公共模块]</p>
 * <p>Description: 消息处理</p>
 * @version        1.0
 */
public class MessageSourceAdapter {
	@Autowired
	private MessageSource messageSource;

	private Locale locale = Locale.CHINA;

	/**
	 * 消息映射				               
	 */
	public String getMessage(String msgKey) {
		try {
			return this.messageSource.getMessage(msgKey, null, locale);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 消息映射(带占位符 , eg. {0}和{1}必须相等)					               
	*/
	public String getMessage(String msgKey, Object[] param) {
		try {
			return this.messageSource.getMessage(msgKey, param, locale);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 业务参数翻译
	 * eg. getMessage("isAvailable", "0")，同getMessage("isAvailable_0")            
	*/
	public String getMessage(String key, Object value) {
		if (value == null) {
			return "";
		}
		return getMessage(key + "_" + value);
	}

	/**
	 * 对Map进行多字段翻译				               
	*/
	public void translateMap(Map source, String... keys) {
		for (String key : keys) {
			source.put(key + "Name", getMessage(key, source.get(key)));
		}
	}
}
